package com.kintreda.ieco_server.module.file.web;

import com.kintreda.ieco_server.bean.document.Attachment;
import com.kintreda.ieco_server.bean.document.Document;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.file.service.IAttachmentService;
import com.kintreda.ieco_server.module.file.service.IDocumentService;
import com.kintreda.ieco_server.util.file.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.file
 * @Author ZhongYao_H
 * @Date 2019/5/2:12:03
 * @Year 2019 Month 05 Day 02
 * @Date_zh 2019 年 五月 星期四
 */
@Controller
@RequestMapping("/file")
public class FileController {
    @Autowired
    private IAttachmentService attachmentService;
    @Autowired
    private IDocumentService documentService;


    @RequestMapping("/test")
    @ResponseBody
    public Object test(){

        List<String> list = new LinkedList<>();
        list.add("黄红药");
        list.add("无止息");
        list.add("wuzixu");
        list.add("测试nui-app调用接口");
        System.out.println("\"访问请求已处理\" = " + "访问请求已处理");
        System.out.println("\"访问请求已处理\" = " + "访问请求已处理");
        return list;
    }


    @RequestMapping(value = "/uploadimage",method = RequestMethod.POST)
    @ResponseBody
    public R uploadImage(MultipartFile file,String owner) throws IOException {
        if (owner==null) {
            owner = "";
        }
        Map<String, String> result = FileUploadUtil.uploadImage(file);
        String code = result.get("code");
        if ("500".equals(code)){
            return R.error(result.get("msg"));
        }
        Attachment attachment = new Attachment();
        Document   document   = new Document();
        attachment.setName(result.get("fileName"));
        attachment.setPath(result.get("path"));
        attachment.setSize(result.get("size"));
        String attachmentId = attachmentService.save(attachment).toString();
        document.setAttachId(attachmentId);
        document.setOwner(owner);
        document.setType(1);
        String docId = documentService.save(document).toString();
        R r = R.ok();
        r.put("id",docId);
        r.put("msg",result.get("msg"));
        return r;
    }

    /**
     *@explain : 图片回显
     *
     * @param response
     *@return  :void
     *@author : HuangZhongYao
     *@time   : 2019/4/13 2:55
     */
    @RequestMapping(value = "/IoReadImage", method = RequestMethod.GET)
    public void IoReadImage(String docId , HttpServletResponse response) throws IOException {

        Document document = documentService.get(docId);
        if (document==null){
            return;
        }

        Attachment attachment = attachmentService.get(document.getAttachId());
        if (attachment==null){
            return;
        }

        System.out.println("显示图片路径 = " + attachment.getPath());
        ServletOutputStream out = null;
        FileInputStream ips = null;
        try {
            //获取图片存放路径
            String imgPath =  attachment.getPath();
            ips = new FileInputStream(new File(imgPath));
            response.setContentType("multipart/form-data");
            out = response.getOutputStream();
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = ips.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.close();
            ips.close();
        }
    }
}
